package com.config;

import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;

@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {
    @Autowired
    private LoginHandlerInterceptor loginHandlerInterceptor;

    @Autowired
    private AppConfig appConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
    @Bean
    public WebMvcConfigurerAdapter WebMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            /**添加拦截器*/
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(loginHandlerInterceptor).addPathPatterns("/admin/**")
                        .addPathPatterns("/user/center")
                        .addPathPatterns("/user/perfectinfo")
                        .excludePathPatterns("/admin").
                        excludePathPatterns("/returnurl/*").
                        excludePathPatterns("/pic/**").
                        excludePathPatterns("/admin/login");
            }

            /**静态资源处理*/
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                //appConfig.getResPhysicalPath() 这表示项目所在的文件夹，下面会有介绍
                String webPath = appConfig.getResPhysicalPath();

                File file = new File(webPath + "/pic/");
                if (!file.exists()) {
                    file.mkdirs();
                }
                System.out.println("静态资源处理：" + webPath + "/pic/");
                registry.addResourceHandler("/pic/**").addResourceLocations("file:" + webPath + "/pic/");
                super.addResourceHandlers(registry);
            }
        };
        return adapter;
    }

    @Bean
    public IdWorker idWorker(){
        return  new IdWorker();
    }

    @Bean
    public RedisTemplate<?, ?> getRedisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<?, ?> template = new StringRedisTemplate(connectionFactory);
        /**
         * 使用Jackson2JsonRedisSerialize 替换默认序列化
         */
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        /**
         * 设置value的序列化规则和 key的序列化规则
         */
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        /*
         *   加入有关 HashValue的序列化配置
         */
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }




}